//
//  dataBaseHandle.m
//  UI14_豆瓣电影更新版
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "dataBaseHandle.h"
#import "ActivityModel.h"
@implementation dataBaseHandle
+ (dataBaseHandle *)sharedDataBaseHandle {
    // 静态修饰指针
    static dataBaseHandle *handle = nil;
    // 线程只执行一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[dataBaseHandle alloc] init];
    });
    return handle;
}
- (void)openDB {
    // 沙盒路径
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 拼接数据库路径
    NSString *dbPath = [document stringByAppendingPathComponent:@"douban.sqlite"];
    NSLog(@"%@", dbPath);
    // 打开数据库
    int result = sqlite3_open(dbPath.UTF8String, &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    }else {
        NSLog(@"打开失败");
    }
    
}
- (void)creatTable {
    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists Student(number integer primary key autoincrement, title text, image text, participant_count text, wisher_count text, end_time text, begin_time text, address text, owner text, category_name text, content tetx)"];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"建表成功");
    }else {
        NSLog(@"建表失败");
    }
}
- (void)insertModel:(ActivityModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Student(title, image, participant_count, wisher_count, end_time, begin_time, address, owner, category_name, content) values('%@','%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", model.title, model.image, model.participant_count, model.wisher_count, model.end_time, model.begin_time, model.address, model.owner, model.category_name, model.content];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else {
        NSLog(@"插入失败");
    }
}
- (void)deleteModel:(ActivityModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Student where title = '%@'", model.title];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else {
        NSLog(@"删除失败");
    }
}
- (void)deleteAll {
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Student"];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除所有");
    }else {
        NSLog(@"删除失败");
    }
}
- (void)updateModel:(ActivityModel *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"update Student set content = '%@' where title = 'hs'", model.content];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"更新成功");
    }else {
        NSLog(@"更新失败");
    }
}
- (NSMutableArray *)selectModel {
    NSString *sqlStr = [NSString stringWithFormat:@"select * from Student"];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    NSMutableArray *arr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            const unsigned char *image = sqlite3_column_text(stmt, 2);
            const unsigned char *participant_count = sqlite3_column_text(stmt, 3);
            const unsigned char *wisher_count = sqlite3_column_text(stmt, 4);
            const unsigned char *end_time = sqlite3_column_text(stmt, 5);
            const unsigned char *begin_time = sqlite3_column_text(stmt, 6);
            const unsigned char *address = sqlite3_column_text(stmt, 7);
            const unsigned char *owner = sqlite3_column_text(stmt, 8);
            const unsigned char *category_name = sqlite3_column_text(stmt, 9);
            const unsigned char *content = sqlite3_column_text(stmt, 10);
            ActivityModel *model =[ActivityModel activity];
            model.title = [NSString stringWithUTF8String:(const char *)title];
            model.image = [NSString stringWithUTF8String:(const char *)image];
            model.participant_count = [NSString stringWithUTF8String:(const char *)participant_count];
            model.wisher_count = [NSString stringWithUTF8String:(const char *)wisher_count];
            model.end_time = [NSString stringWithUTF8String:(const char *)end_time];
            model.begin_time = [NSString stringWithUTF8String:(const char *)begin_time];
            model.address = [NSString stringWithUTF8String:(const char *)address];
            model.owner = [NSString stringWithUTF8String:(const char *)owner];
            model.category_name = [NSString stringWithUTF8String:(const char *)category_name];
            model.content = [NSString stringWithUTF8String:(const char *)content];
            
            
            
            [arr addObject:model.title];
        }
    
    }else {
        NSLog(@"查找失败");
    }
    return arr;
}
@end
